<!-- 新闻主页 -->
<template>
  <div class="container">
    <div
      style="display: flex; justify-content: space-between; align-items: center"
    >
      <span class="news">全部服务事项</span>
      <el-input
        v-model="searchKey"
        style="width: 240px"
        size="large"
        placeholder="搜索服务事项"
        @keyup.enter="getTaskList"
      />
      <div>
        <el-button
          v-if="userStore.userInfo?.role == 0"
          type="primary"
          @click="addTask"
          >新增可办理事项</el-button
        >
        <el-button
          v-if="userStore.userInfo?.role == 0"
          type="primary"
          @click="
            () => {
              router.push('/things/list');
            }
          "
          >申请记录列表</el-button
        >
      </div>
    </div>
    <div class="workCard">
      <el-card
        v-for="item in thingsList"
        :key="item.id"
        style="
          max-width: 22%;
          min-width: 22%;
          cursor: pointer;
          margin-bottom: 50px;
          margin-left: 25px;
          margin-right: 25px;
        "
        @click="goApply(item.name, item.id, item.template)"
      >
        <div style="display: flex; justify-content: space-between">
          <div class="newsTitle">{{ item.name }}</div>
          <div v-if="userStore.userInfo?.role == 0">
            <el-link type="primary" :underline="false" @click="editTask(item)"
              >编辑</el-link
            >
            <el-link
              style="margin-left: 10px"
              type="primary"
              :underline="false"
              @click="deleteTask(item.id)"
              >删除</el-link
            >
          </div>
        </div>

        <div class="newsText">
          {{ item.description }}
        </div>
      </el-card>
    </div>
    <div style="margin-top: 20px; display: flex; justify-content: flex-end">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        v-model:current-page="page"
        :pageSize="pageSize"
        @current-change="pageChange"
      />
    </div>
  </div>

  <el-dialog v-model="dialogVisible" title="服务事项管理" width="600">
    <el-form>
      <el-form-item label="事务名称" style="width: 100%">
        <el-input v-model="task.name" style="width: 100%" />
      </el-form-item>
      <el-form-item label="事务概述" style="width: 100%">
        <el-input v-model="task.description" style="width: 100%" />
      </el-form-item>
      <el-form-item label="申请表模板" style="width: 100%">
        <el-upload
          class="upload-demo"
          style="width: 100%"
          v-model:file-list="fileList"
          :show-file-list="true"
          :limit="1"
          drag
          multiple
          :auto-upload="true"
          :http-request="upload"
        >
          <el-icon class="el-icon--upload"><upload-filled /></el-icon>
          <div class="el-upload__text">将文件拽到此处或 <em>点击上传</em></div>
        </el-upload>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {
  reqGetTaskList,
  uploadFile,
  reqAddTask,
  reqUpdateTask,
  reqDeleteTask,
} from "@/api/api";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { useUserStore } from "@/stores/userInfo";
import { UploadFilled } from "@element-plus/icons-vue";
/**用户仓库，拿当前用户信息 */
const userStore = useUserStore();
/**搜索关键词 */
const searchKey = ref("");
/**新闻列表 */
const thingsList = ref([]);
/**路由，用于路由跳转 */
const router = useRouter();

const pageSize = ref(12);
const page = ref(1);
const total = ref(0);

/**当前事务 */
const task = ref();
const dialogVisible = ref(false);
/**文件列表 */
const fileList = ref([]);
/**获取事务列表 */
const getTaskList = async () => {
  const res = await reqGetTaskList({
    page: page.value,
    pageSize: pageSize.value,
    keyword: searchKey.value,
  });
  if (res.code === 200) {
    thingsList.value = res.data.records;
    total.value = res.data.totalRow;
  } else {
    ElMessage.error(res.msg);
  }
};

/**页面加载时获取事务列表 */
onMounted(() => {
  getTaskList();
});

/**分页改变时获取事务列表 */
const pageChange = () => {
  getTaskList();
};

/**新增事务 */
const addTask = () => {
  task.value = {
    name: "",
    description: "",
    cover: "",
    template: "",
  };
  dialogVisible.value = true;
};
/**编辑事务 */
const editTask = (item) => {
  task.value = item;
  dialogVisible.value = true;
  fileList.value = [{ name: "模板" }];
};

const deleteTask = async (id) => {
  const res = await reqDeleteTask(id);
  if (res.code == 200) {
    ElMessage.success("删除成功");
    getTaskList();
  } else {
    ElMessage.error(res.msg);
  }
};

const goApply = (title, id, template) => {
  if (userStore.userInfo?.role == 1) {
    router.push(
      `/things/apply?project=${title}&matterId=${id}&fileUrl=${template}`
    );
  }
};
//新增事务保存
const save = async () => {
  if (task.value.id) {
    const res = await reqUpdateTask(task.value);

    if (res.code == 200) {
      ElMessage.success("修改成功");
      dialogVisible.value = false;
      getTaskList();
    } else {
      ElMessage.error(res.msg);
    }
  } else {
    const res = await reqAddTask(task.value);
    if (res.code == 200) {
      ElMessage.success("添加成功");
      dialogVisible.value = false;
      getTaskList();
    } else {
      ElMessage.error(res.msg);
    }
  }
};
//上传模板
const upload = async () => {
  console.log(fileList.value[0].raw);
  console.log(fileList.value);

  const res = await uploadFile({ file: fileList.value[0].raw });
  if (res.code == 200) {
    // imageUrl.value = res.data;
    task.value.template = res.data;
  } else {
    ElMessage.error(res.msg);
  }
};
</script>

<style lang="scss" scoped>
.container {
  padding: 40px 50px;
  .news {
    font-weight: bold;
    font-size: 18px;
  }
  .workCard {
    margin-top: 44px;
    display: flex;
    // justify-content: space-between;
    flex-wrap: wrap;
  }
  .newsTitle {
    font-weight: bold;
    font-size: 20px;
    // margin-top: 20px;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .newsText {
    color: #4b5563;
    margin-top: 20px;

    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .newsDetail {
    margin-top: 20px;
    padding: 0 20px;
    margin-bottom: 30px;
  }
}
</style>
